      1  import java.awt.Color;

      2

      3  /**

      4     This class demonstrates a generic method for printing arrays.

      5  */

      6  public class PrintDemo

      7  {

      8     public static <E> void print(E[] a)

      9     {

     10        for (E e : a)

     11        {

     12           System.out.print(e + " ");

     13        }

     14        System.out.println();

     15     }

     16

     17     public static void print(int[] a)

     18     {

     19        for (int e : a)

     20        {

     21           System.out.print(e + " ");

     22        }

     23        System.out.println();

     24     }

     25

     26     public static void main(String[] args)

     27     {

     28        String[] words = { "Mary", "had", "a", "little", "lamb" };

     29

     30        Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };

     31

     32        int[] squares = { 1, 4, 9, 16, 25, 36 };

     33

     34        print(words); // Calls print<String>

     35        print(colors); // Calls print<Color>

     36        print(squares); // Calls non-generic print

     37     }

     38  }